import {
  FieldMetadataType,
  RelationOnDeleteAction,
  RelationType,
} from 'twenty-shared/types';

import { type FlatFieldMetadata } from 'src/engine/metadata-modules/flat-field-metadata/types/flat-field-metadata.type';
import { type AllStandardObjectFieldName } from 'src/engine/workspace-manager/twenty-standard-application/types/all-standard-object-field-name.type';
import { createStandardFieldFlatMetadata } from 'src/engine/workspace-manager/twenty-standard-application/utils/create-standard-field-flat-metadata.util';
import { createStandardRelationFieldFlatMetadata } from 'src/engine/workspace-manager/twenty-standard-application/utils/create-standard-relation-field-flat-metadata.util';
import { type StandardFieldMetadataIdByObjectAndFieldName } from 'src/engine/workspace-manager/twenty-standard-application/utils/get-standard-field-metadata-id-by-object-and-field-name.util';

export const buildPersonStandardFlatFieldMetadatas = ({
  createdAt,
  workspaceId,
  standardFieldMetadataIdByObjectAndFieldName,
}: {
  createdAt: Date;
  workspaceId: string;
  standardFieldMetadataIdByObjectAndFieldName: StandardFieldMetadataIdByObjectAndFieldName;
}): Record<AllStandardObjectFieldName<'person'>, FlatFieldMetadata> => ({
  // Base fields from BaseWorkspaceEntity
  id: createStandardFieldFlatMetadata({
    objectName: 'person',
    workspaceId,
    options: {
      fieldName: 'id',
      type: FieldMetadataType.UUID,
      label: 'Id',
      description: 'Id',
      icon: 'Icon123',
      isSystem: true,
      isNullable: false,
      isUIReadOnly: true,
      defaultValue: 'uuid',
      createdAt,
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  createdAt: createStandardFieldFlatMetadata({
    objectName: 'person',
    workspaceId,
    options: {
      fieldName: 'createdAt',
      type: FieldMetadataType.DATE_TIME,
      label: 'Creation date',
      description: 'Creation date',
      icon: 'IconCalendar',
      isNullable: false,
      isUIReadOnly: true,
      defaultValue: 'now',
      settings: {
        displayFormat: 'RELATIVE',
      },
      createdAt,
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  updatedAt: createStandardFieldFlatMetadata({
    objectName: 'person',
    workspaceId,
    options: {
      fieldName: 'updatedAt',
      type: FieldMetadataType.DATE_TIME,
      label: 'Last update',
      description: 'Last time the record was changed',
      icon: 'IconCalendarClock',
      isNullable: false,
      isUIReadOnly: true,
      defaultValue: 'now',
      settings: {
        displayFormat: 'RELATIVE',
      },
      createdAt,
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  deletedAt: createStandardFieldFlatMetadata({
    objectName: 'person',
    workspaceId,
    options: {
      fieldName: 'deletedAt',
      type: FieldMetadataType.DATE_TIME,
      label: 'Deleted at',
      description: 'Date when the record was deleted',
      icon: 'IconCalendarMinus',
      isNullable: true,
      isUIReadOnly: true,
      settings: {
        displayFormat: 'RELATIVE',
      },
      createdAt,
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),

  // Person-specific fields
  name: createStandardFieldFlatMetadata({
    objectName: 'person',
    workspaceId,
    options: {
      fieldName: 'name',
      type: FieldMetadataType.FULL_NAME,
      label: 'Name',
      description: "Contact's name",
      icon: 'IconUser',
      isNullable: true,
      createdAt,
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  email: createStandardFieldFlatMetadata({
    objectName: 'person',
    workspaceId,
    options: {
      fieldName: 'email',
      type: FieldMetadataType.TEXT,
      label: 'Email',
      description: "Contact's Email (deprecated)",
      icon: 'IconMail',
      isNullable: true,
      createdAt,
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  emails: createStandardFieldFlatMetadata({
    objectName: 'person',
    workspaceId,
    options: {
      fieldName: 'emails',
      type: FieldMetadataType.EMAILS,
      label: 'Emails',
      description: "Contact's Emails",
      icon: 'IconMail',
      isNullable: true,
      isUnique: true,
      settings: {
        maxNumberOfValues: 1,
      },
      createdAt,
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  linkedinLink: createStandardFieldFlatMetadata({
    objectName: 'person',
    workspaceId,
    options: {
      fieldName: 'linkedinLink',
      type: FieldMetadataType.LINKS,
      label: 'Linkedin',
      description: "Contact's Linkedin account",
      icon: 'IconBrandLinkedin',
      isNullable: true,
      createdAt,
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  xLink: createStandardFieldFlatMetadata({
    objectName: 'person',
    workspaceId,
    options: {
      fieldName: 'xLink',
      type: FieldMetadataType.LINKS,
      label: 'X',
      description: "Contact's X/Twitter account",
      icon: 'IconBrandX',
      isNullable: true,
      createdAt,
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  jobTitle: createStandardFieldFlatMetadata({
    objectName: 'person',
    workspaceId,
    options: {
      fieldName: 'jobTitle',
      type: FieldMetadataType.TEXT,
      label: 'Job Title',
      description: "Contact's job title",
      icon: 'IconBriefcase',
      isNullable: true,
      createdAt,
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  phone: createStandardFieldFlatMetadata({
    objectName: 'person',
    workspaceId,
    options: {
      fieldName: 'phone',
      type: FieldMetadataType.TEXT,
      label: 'Phone',
      description: "Contact's phone number (deprecated)",
      icon: 'IconPhone',
      isNullable: true,
      createdAt,
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  phones: createStandardFieldFlatMetadata({
    objectName: 'person',
    workspaceId,
    options: {
      fieldName: 'phones',
      type: FieldMetadataType.PHONES,
      label: 'Phones',
      description: "Contact's phone numbers",
      icon: 'IconPhone',
      isNullable: true,
      settings: {
        maxNumberOfValues: 1,
      },
      createdAt,
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  city: createStandardFieldFlatMetadata({
    objectName: 'person',
    workspaceId,
    options: {
      fieldName: 'city',
      type: FieldMetadataType.TEXT,
      label: 'City',
      description: "Contact's city",
      icon: 'IconMap',
      isNullable: true,
      createdAt,
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  avatarUrl: createStandardFieldFlatMetadata({
    objectName: 'person',
    workspaceId,
    options: {
      fieldName: 'avatarUrl',
      type: FieldMetadataType.TEXT,
      label: 'Avatar',
      description: "Contact's avatar",
      icon: 'IconFileUpload',
      isSystem: true,
      isNullable: true,
      createdAt,
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  position: createStandardFieldFlatMetadata({
    objectName: 'person',
    workspaceId,
    options: {
      fieldName: 'position',
      type: FieldMetadataType.POSITION,
      label: 'Position',
      description: 'Person record Position',
      icon: 'IconHierarchy2',
      isSystem: true,
      isNullable: false,
      defaultValue: 0,
      createdAt,
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  createdBy: createStandardFieldFlatMetadata({
    objectName: 'person',
    workspaceId,
    options: {
      fieldName: 'createdBy',
      type: FieldMetadataType.ACTOR,
      label: 'Created by',
      description: 'The creator of the record',
      icon: 'IconCreativeCommonsSa',
      isUIReadOnly: true,
      isNullable: false,
      createdAt,
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),

  // Relation fields
  company: createStandardRelationFieldFlatMetadata({
    objectName: 'person',
    workspaceId,
    options: {
      fieldName: 'company',
      label: 'Company',
      description: "Contact's company",
      icon: 'IconBuildingSkyscraper',
      isNullable: true,
      createdAt,
      targetObjectName: 'company',
      targetFieldName: 'people',
      settings: {
        relationType: RelationType.MANY_TO_ONE,
        onDelete: RelationOnDeleteAction.SET_NULL,
        joinColumnName: 'companyId',
      },
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  pointOfContactForOpportunities: createStandardRelationFieldFlatMetadata({
    objectName: 'person',
    workspaceId,
    options: {
      fieldName: 'pointOfContactForOpportunities',
      label: 'Opportunities',
      description:
        'List of opportunities for which that person is the point of contact',
      icon: 'IconTargetArrow',
      isNullable: true,
      createdAt,
      targetObjectName: 'opportunity',
      targetFieldName: 'pointOfContact',
      settings: {
        relationType: RelationType.ONE_TO_MANY,
      },
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  taskTargets: createStandardRelationFieldFlatMetadata({
    objectName: 'person',
    workspaceId,
    options: {
      fieldName: 'taskTargets',
      label: 'Tasks',
      description: 'Tasks tied to the contact',
      icon: 'IconCheckbox',
      isUIReadOnly: true,
      isNullable: true,
      createdAt,
      targetObjectName: 'taskTarget',
      targetFieldName: 'person',
      settings: {
        relationType: RelationType.ONE_TO_MANY,
      },
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  noteTargets: createStandardRelationFieldFlatMetadata({
    objectName: 'person',
    workspaceId,
    options: {
      fieldName: 'noteTargets',
      label: 'Notes',
      description: 'Notes tied to the contact',
      icon: 'IconNotes',
      isUIReadOnly: true,
      isNullable: true,
      createdAt,
      targetObjectName: 'noteTarget',
      targetFieldName: 'person',
      settings: {
        relationType: RelationType.ONE_TO_MANY,
      },
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  favorites: createStandardRelationFieldFlatMetadata({
    objectName: 'person',
    workspaceId,
    options: {
      fieldName: 'favorites',
      label: 'Favorites',
      description: 'Favorites linked to the contact',
      icon: 'IconHeart',
      isSystem: true,
      isNullable: true,
      createdAt,
      targetObjectName: 'favorite',
      targetFieldName: 'person',
      settings: {
        relationType: RelationType.ONE_TO_MANY,
      },
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  attachments: createStandardRelationFieldFlatMetadata({
    objectName: 'person',
    workspaceId,
    options: {
      fieldName: 'attachments',
      label: 'Attachments',
      description: 'Attachments linked to the contact.',
      icon: 'IconFileImport',
      isSystem: true,
      isNullable: true,
      createdAt,
      targetObjectName: 'attachment',
      targetFieldName: 'person',
      settings: {
        relationType: RelationType.ONE_TO_MANY,
      },
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  messageParticipants: createStandardRelationFieldFlatMetadata({
    objectName: 'person',
    workspaceId,
    options: {
      fieldName: 'messageParticipants',
      label: 'Message Participants',
      description: 'Message Participants',
      icon: 'IconUserCircle',
      isSystem: true,
      isNullable: true,
      createdAt,
      targetObjectName: 'messageParticipant',
      targetFieldName: 'person',
      settings: {
        relationType: RelationType.ONE_TO_MANY,
      },
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  calendarEventParticipants: createStandardRelationFieldFlatMetadata({
    objectName: 'person',
    workspaceId,
    options: {
      fieldName: 'calendarEventParticipants',
      label: 'Calendar Event Participants',
      description: 'Calendar Event Participants',
      icon: 'IconCalendar',
      isSystem: true,
      isNullable: true,
      createdAt,
      targetObjectName: 'calendarEventParticipant',
      targetFieldName: 'person',
      settings: {
        relationType: RelationType.ONE_TO_MANY,
      },
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  timelineActivities: createStandardRelationFieldFlatMetadata({
    objectName: 'person',
    workspaceId,
    options: {
      fieldName: 'timelineActivities',
      label: 'Events',
      description: 'Events linked to the person',
      icon: 'IconTimelineEvent',
      isSystem: true,
      isNullable: true,
      createdAt,
      targetObjectName: 'timelineActivity',
      targetFieldName: 'person',
      settings: {
        relationType: RelationType.ONE_TO_MANY,
      },
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
  searchVector: createStandardFieldFlatMetadata({
    objectName: 'person',
    workspaceId,
    options: {
      fieldName: 'searchVector',
      type: FieldMetadataType.TS_VECTOR,
      label: 'Search vector',
      description: 'Field used for full-text search',
      icon: 'IconUser',
      isSystem: true,
      isNullable: true,
      createdAt,
    },
    standardFieldMetadataIdByObjectAndFieldName,
  }),
});
